package day07;

public class 买卖股票的最佳时机含冷冻期 {
    /*public int maxProfit(int[] prices) {
        int n = prices.length;
        // 定义dp表
        int[][] dp = new int[n][4];
        // 初始化
        dp[0][0] = -prices[0];
        // 进行for循环
        for (int i = 1; i < n; i++) {
            int tmp = Math.max(dp[i - 1][0], dp[i - 1][3] - prices[i]);
            dp[i][0] = Math.max(tmp, dp[i - 1][1] - prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
            dp[i][2] = dp[i - 1][0] + prices[i];
            dp[i][3] = dp[i - 1][2];
        }
        int ret1 = Math.max(dp[n - 1][0], dp[n - 1][1]);
        int ret2 = Math.max(dp[n - 1][2], dp[n - 1][3]);
        return Math.max(ret1, ret2);
    }*/
    public int maxProfit(int[] prices) {
        int n = prices.length;
        // 定义dp表
        int[][] dp = new int[n][3];
        // 初始化
        dp[0][0] = -prices[0];
        // 进行for循环
        for (int i = 1; i < n; i++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][2]);
            dp[i][2] = dp[i - 1][0] + prices[i];
        }
        int ret1 = Math.max(dp[n - 1][0], dp[n - 1][1]);
        int ret2 = Math.max(ret1, dp[n - 1][2]);
        return Math.max(ret1, ret2);
    }
}
